﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StudentsDB
{
    public partial class Group : Form
    {
        public Group()
        {
            InitializeComponent();
        }

        private GroupStringEdit GroupStringEditForm, GroupStringCreateForm;
        private GroupsService service;
        private IList<Groups> list;
        private Groups res_tmp= new Groups();
        private int MaxIdGroup;

        protected void ComboBoxListValues()
        {
            NumberComboBox.Items.Clear();
           for (int i = 0; i < GroupGridView.Rows.Count; i++)
           {
               NumberComboBox.Items.Add(GroupGridView.Rows[i].Cells[1].Value.ToString());
           }
        }
       
        private void Group_Load(object sender, EventArgs e)
        {
            service = new GroupsService();
            list = service.GetAllGroups();
            
            GroupGridView.Rows.Add(list.Count);
            for (int i = 0; i < list.Count; i++)
            {
                GroupGridView.Rows[i].Cells[0].Value = i+1;
                GroupGridView.Rows[i].Cells[1].Value = list[i].Code;
                GroupGridView.Rows[i].Cells[2].Value = new Bitmap(@"D:\Kate\Documents\Visual Studio 2010\Projects\StudentsDB\Icons\Toolbars\ObjectEdit.ico");
                GroupGridView.Rows[i].Cells[3].Value = new Bitmap(@"D:\Kate\Documents\Visual Studio 2010\Projects\StudentsDB\Icons\Toolbars\Delete.ico");
            }
            MaxIdGroup = list.Count;
            ComboBoxListValues();
        }

        private void CreateStripButton_Click(object sender, EventArgs e)
        {
            /*MaxIdGroup++;
            res_tmp.Id = MaxIdGroup;
            res_tmp.Code = 8201;*/
            //service.AddGroups(res_tmp);
            //HibernateTemplate.GetSession().SaveOrUpdateCopy(res_tmp);
            /*HibernateTemplate.GetSession().CreateSQLQuery("Insert into Groups Values(" + MaxIdGroup.ToString() + "," +
                                                          "8201);");
            button1.Text = "HibernateTemplate.GetSession() Done!";*/

            GroupStringCreateForm = new GroupStringEdit(MaxIdGroup,service);
            GroupStringCreateForm.Text = "Группа (Создание)";
            GroupStringCreateForm.AllActions(false);
            GroupStringCreateForm.ShowDialog();/**/
            if (GroupStringCreateForm == null||GroupStringCreateForm.IsDisposed)
            {
                
                GroupStringCreateForm.Show();
                
                return;
            }
            /*if (!GroupStringCreateForm.IsDisposed)
            {
                GroupStringCreateForm.Activate();
            }
            else
            {
                GroupStringCreateForm = new GroupStringEdit();
                GroupStringCreateForm.Text = "Группа (Создание)";
                GroupStringCreateForm.AllActions(false);
                GroupStringCreateForm.Show();
            }*/
        }

        private void CreateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateStripButton_Click(sender, e);
        }

        private void EditToolStripMenuItem_Click(object sender, EventArgs e)
        {
           GroupStringEditForm = new GroupStringEdit();
            GroupStringEditForm.Text = "Группа (Редактирование)";
            GroupStringEditForm.FieldNumber(GroupGridView.CurrentRow.Cells[1].Value.ToString());
            GroupStringEditForm.ShowDialog();
            if (GroupStringEditForm == null || GroupStringEditForm.IsDisposed)
            {
                GroupStringEditForm.ShowDialog();
                return;
            }
            if (!GroupStringEditForm.IsDisposed)
            {
                GroupStringEditForm.Activate();
            }
            /*/*else
            {
                GroupStringEditForm = new GroupStringEdit();
                GroupStringEditForm.Text = "Группа (Редактирование)";
                GroupStringEditForm.FieldNumber(GroupGridView.CurrentRow.Cells[1].Value.ToString());
                GroupStringEditForm.Show();
            } /**/
        }

        private void CreateAA_Click(object sender, EventArgs e)
        {
            CreateStripButton_Click(sender, e);
        }

        private void EditAA_Click(object sender, EventArgs e)
        {
            EditToolStripMenuItem_Click(sender, e);
        }

       private void GroupGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            GroupGridView.Rows[GroupGridView.Rows.Count - 1].Cells[2].Value = new Bitmap(@"D:\Kate\Documents\Visual Studio 2010\Projects\StudentsDB\Icons\Toolbars\ObjectEdit.ico");
            GroupGridView.Rows[GroupGridView.Rows.Count - 1].Cells[3].Value = new Bitmap(@"D:\Kate\Documents\Visual Studio 2010\Projects\StudentsDB\Icons\Toolbars\Delete.ico");
            
        }

       private void GroupGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
           if (GroupGridView.CurrentRow.Cells[2].Selected)
           {
               EditToolStripMenuItem_Click(sender, e);
               return;
           }
           if (GroupGridView.CurrentRow.Cells[3].Selected)
           {
               if(MessageBox.Show("Вы действительно хотите удалить выбранную строку?", "Удаление строки", MessageBoxButtons.OKCancel,/*MessageBoxIcon.Warning*/ MessageBoxIcon.Question)==DialogResult.OK)
               {
                   //DeleteGroups
               }
           }
       }

       private void Search_Click(object sender, EventArgs e)
       {
           int tmp=8101;
           if (NumberComboBox.Text=="")
           {
               MessageBox.Show("Введена пустая строка! На ввод требуется целочисленный номер.", "Ошибка поиска", MessageBoxButtons.OK, MessageBoxIcon.Error);
               return;
           }
           try
           {
               tmp = Int32.Parse(NumberComboBox.Text);
           }
           catch
           {
               MessageBox.Show("Ошибка ввода! На ввод требуется целочисленный номер.", "Ошибка поиска", MessageBoxButtons.OK, MessageBoxIcon.Error);
               NumberComboBox.Text = "";
               return;
           }
           res_tmp = service.GetGroupsByCode(tmp);
           if (res_tmp == null || res_tmp.Id == null)
           {
               GroupGridView.Visible = false;
               if (MessageBox.Show("Поиск результатов не дал.", "Результат поиска") == DialogResult.OK) { GroupGridView.Visible = true; }
           }
           else
           {
               GroupGridView.Rows.Clear();
               GroupGridView.Rows.Add();
               GroupGridView.Rows[0].Cells[0].Value = 1;
               GroupGridView.Rows[0].Cells[1].Value = res_tmp.Code;
               GroupGridView.Rows[0].Cells[2].Value =
                   new Bitmap(
                       @"D:\Kate\Documents\Visual Studio 2010\Projects\StudentsDB\Icons\Toolbars\ObjectEdit.ico");
               GroupGridView.Rows[0].Cells[3].Value =
                   new Bitmap(@"D:\Kate\Documents\Visual Studio 2010\Projects\StudentsDB\Icons\Toolbars\Delete.ico");
           }
       }

       private void button1_Click(object sender, EventArgs e)
       {
           list = service.GetAllGroups();

           GroupGridView.Rows.Add(list.Count);
           for (int i = 0; i < list.Count; i++)
           {
               GroupGridView.Rows[i].Cells[0].Value = i + 1;
               GroupGridView.Rows[i].Cells[1].Value = list[i].Code;
               GroupGridView.Rows[i].Cells[2].Value = new Bitmap(@"D:\Kate\Documents\Visual Studio 2010\Projects\StudentsDB\Icons\Toolbars\ObjectEdit.ico");
               GroupGridView.Rows[i].Cells[3].Value = new Bitmap(@"D:\Kate\Documents\Visual Studio 2010\Projects\StudentsDB\Icons\Toolbars\Delete.ico");
           }
           MaxIdGroup = list.Count;
           ComboBoxListValues();
       }
    }
}
